package com.hkw.singlecityidpicker.popup;

import android.content.res.AssetManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hkw.singlecityidpicker.R;
import com.hkw.singlecityidpicker.areapickerview.AddressBean;
import com.hkw.singlecityidpicker.areapickerview.AreaAdapter;
import com.hkw.singlecityidpicker.areapickerview.CityAdapter;
import com.hkw.singlecityidpicker.areapickerview.ProvinceAdapter;
import com.hkw.singlecityidpicker.configure.PickerOptions;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class AddressUserPopup extends BottomPopupView implements View.OnClickListener {


    protected PickerOptions mPickerOptions;
    private MyOnClickListener myOnClickListener;

    private TextView tvClose;
    private WheelView wheel1;
    private WheelView wheel2;
    private WheelView wheel3;
    private TextView tvConfirm;


    private List<AddressBean> addressBeans;//一级地址
    private CityAdapter cityAdapter;
    private AreaAdapter areaAdapter;
    private int provinceId;
    private int cityId;

    private String provinceName, cityName, areaName, areaCode;


    private String getCityJson() {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = getContext().getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open("city2.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


    public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    /*public AddressUserPopup(@NonNull Context context) {
        super(context);
    }*/
    public AddressUserPopup(PickerOptions pickerOptions) {
        super(pickerOptions.context);
        mPickerOptions = pickerOptions;
        //initView(pickerOptions.context);
        shower();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.ui_my_popup_user_address;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        tvClose = findViewById(R.id.tv_close);
        wheel1 = findViewById(R.id.wheel1);
        wheel2 = findViewById(R.id.wheel2);
        wheel3 = findViewById(R.id.wheel3);
        tvConfirm = findViewById(R.id.tv_confirm);
        TextView tvTitle = findViewById(R.id.tv_title);
        RelativeLayout rv_top_bar = findViewById(R.id.rv_top_bar);
        tvClose.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);

        //设置文字
        tvConfirm.setText(TextUtils.isEmpty(mPickerOptions.textContentConfirm) ? mPickerOptions.context.getResources().getString(R.string.pickerview_submit) : mPickerOptions.textContentConfirm);
        tvClose.setText(TextUtils.isEmpty(mPickerOptions.textContentCancel) ? mPickerOptions.context.getResources().getString(R.string.pickerview_cancel) : mPickerOptions.textContentCancel);
        tvTitle.setText(TextUtils.isEmpty(mPickerOptions.textContentTitle) ? "" : mPickerOptions.textContentTitle);//默认为空

        //设置color
        tvConfirm.setTextColor(mPickerOptions.textColorConfirm);
        tvClose.setTextColor(mPickerOptions.textColorCancel);
        tvTitle.setTextColor(mPickerOptions.textColorTitle);
        rv_top_bar.setBackgroundColor(mPickerOptions.bgColorTitle);

        //设置文字大小
        tvConfirm.setTextSize(mPickerOptions.textSizeSubmitCancel);
        tvClose.setTextSize(mPickerOptions.textSizeSubmitCancel);
        tvTitle.setTextSize(mPickerOptions.textSizeTitle);

        // ----滚轮布局
        final LinearLayout optionsPicker =  findViewById(R.id.optionspicker);
        optionsPicker.setBackgroundColor(mPickerOptions.bgColorWheel);



        //自定义省市县滑轮
        addressBeans = new Gson().fromJson(getCityJson(), new TypeToken<List<AddressBean>>() {
        }.getType());
        provinceName = addressBeans.get(0).getLabel();//默认省
        cityName = addressBeans.get(0).getChildren().get(0).getLabel();//默认市
        areaName = addressBeans.get(0).getChildren().get(0).getChildren().get(0).getLabel();//默认区县
        areaCode = addressBeans.get(0).getChildren().get(0).getChildren().get(0).getValue();//默认区县代码
        wheel1.setAdapter(new ProvinceAdapter(getContext(), addressBeans));
        wheel1.setCyclic(mPickerOptions.cyclic1);
        wheel1.setCurrentItem(mPickerOptions.option1);//默认滑动选项

        wheel1.setDividerColor(mPickerOptions.dividerColor);
        wheel1.setTextColorCenter(mPickerOptions.textColorCenter);
        wheel1.setTextSize(mPickerOptions.textSizeContent);
        wheel1.setItemsVisibleCount(mPickerOptions.visibleCount);
        wheel1.setTextXOffset(mPickerOptions.x_offset_one);
        //wheel1.setTotalScrollY(0);
        wheel1.setTextColorOut(mPickerOptions.textColorOut);
        wheel1.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier);
        wheel1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                provinceId = index;
                cityAdapter.setCityBeans(addressBeans.get(index).getChildren());
                wheel2.setCurrentItem(mPickerOptions.option2);
                areaAdapter.setAreaBeans(addressBeans.get(index).getChildren().get(0).getChildren());
                wheel3.setCurrentItem(mPickerOptions.option3);
                provinceName = addressBeans.get(index).getLabel();
                cityName = addressBeans.get(index).getChildren().get(0).getLabel();
                areaName = addressBeans.get(provinceId).getChildren().get(0).getChildren().get(0).getLabel();
                areaCode = addressBeans.get(provinceId).getChildren().get(0).getChildren().get(0).getValue();
            }
        });


        cityAdapter = new CityAdapter(getContext(), addressBeans.get(0).getChildren());

        wheel2.setAdapter(cityAdapter);
        wheel2.setCyclic(mPickerOptions.cyclic2);
        wheel2.setCurrentItem(mPickerOptions.option2);//默认滑动选项
        wheel2.setDividerColor(mPickerOptions.dividerColor);
        wheel2.setTextColorCenter(mPickerOptions.textColorCenter);
        wheel2.setTextSize(mPickerOptions.textSizeContent);
        wheel2.setItemsVisibleCount(mPickerOptions.visibleCount);
        wheel2.setTextXOffset(mPickerOptions.x_offset_two);
        //wheel2.setTotalScrollY(0);
        wheel2.setTextColorOut(mPickerOptions.textColorOut);
        wheel2.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier);
        wheel2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                cityId = index;
//                ToolsUtils.toast(getContext(), addressBeans.get(provinceId).getChildren().get(index).getLabel());
                areaAdapter.setAreaBeans(addressBeans.get(provinceId).getChildren().get(index).getChildren());
                wheel3.setCurrentItem(mPickerOptions.option3);
                provinceName = addressBeans.get(provinceId).getLabel();
                cityName = addressBeans.get(provinceId).getChildren().get(index).getLabel();
                areaName = addressBeans.get(provinceId).getChildren().get(index).getChildren().get(0).getLabel();
                areaCode = addressBeans.get(provinceId).getChildren().get(index).getChildren().get(0).getValue();
            }
        });


        areaAdapter = new AreaAdapter(getContext(), addressBeans.get(0).getChildren().get(0).getChildren());

        wheel3.setAdapter(areaAdapter);
        wheel3.setCyclic(mPickerOptions.cyclic3);
        wheel3.setCurrentItem(mPickerOptions.option3);//默认滑动选项
        wheel3.setDividerColor(mPickerOptions.dividerColor);
        wheel3.setTextColorCenter(mPickerOptions.textColorCenter);
        wheel3.setTextSize(mPickerOptions.textSizeContent);
        wheel3.setItemsVisibleCount(mPickerOptions.visibleCount);
        wheel3.setTextXOffset(mPickerOptions.x_offset_three);
        //wheel3.setTotalScrollY(0);
        wheel3.setTextColorOut(mPickerOptions.textColorOut);
        wheel3.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier);
        wheel3.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                provinceName = addressBeans.get(provinceId).getLabel();
                cityName = addressBeans.get(provinceId).getChildren().get(cityId).getLabel();
                areaName = addressBeans.get(provinceId).getChildren().get(cityId).getChildren().get(index).getLabel();
                areaCode = addressBeans.get(provinceId).getChildren().get(cityId).getChildren().get(index).getValue();

            }
        });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_close) {
            dismiss();
        } else if (id == R.id.tv_confirm) {
            if (null != mPickerOptions.optionsSelectListener) {
                mPickerOptions.optionsSelectListener.onOptionsSelect(provinceName + cityName + areaName, provinceName, cityName, areaName, areaCode);
            }
            dismiss();
            /*if (null != myOnClickListener) {

                myOnClickListener.selected(provinceName + cityName + areaName, provinceName, cityName, areaName, areaCode);
            }*/

        }
    }


    public void shower() {
        //super.show();
        if (!super.isShow()) {
            new XPopup.Builder(mPickerOptions.context).asCustom(this).show();
        }
    }


    public interface MyOnClickListener {
        void selected(String address, String province, String city, String area, String areaId);
    }

}
